﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace Mes_app
{
    public partial class Users_Form : Form
    {

        public Users_Form()
        {
            InitializeComponent();
        }

        private void Btn_query_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void Query()
        {
            int active;
            if (CheckBox_active.Checked)
            {
              active = 0;
            }
            else
            { active = 1; 
            }

            string sqlValue = "SELECT a.id,a.login_user_id,a.login_user_name,a.group_power_no,a.group_power_name " +
                " FROM login_user a  " +
                " WHERE 1=1 " +
                " and  a.active = @active " +
                " and  a.login_user_id like '%'+ case when @login_user_id <> '' then @login_user_id else a.login_user_id end  +'%' " +
                " and  a.login_user_name like '%'+ case when @login_user_name <> '' then @login_user_name else a.login_user_name end +'%' " +
                " ORDER BY a.id DESC ";
            SqlParameter[] parameters = {
             new SqlParameter("@login_user_id", TextBox_login_user_id.Text),
             new SqlParameter("@login_user_name", TextBox_login_user_name.Text),
             new SqlParameter("@active", active),
            };
            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);

            DataGV.DataSource = ds.Tables[0];
            DataGV.Columns[0].HeaderText = "ID";
            DataGV.Columns[1].HeaderText = "用户ID";
            DataGV.Columns[2].HeaderText = "姓名";
            DataGV.Columns[3].HeaderText = "组编号";
            DataGV.Columns[4].HeaderText = "权限组";

            DataGV.Columns[0].Width = 70;
            DataGV.Columns[1].Width = 90;
            DataGV.Columns[2].Width = 150;
            DataGV.Columns[3].Width = 70;
            DataGV.Columns[4].Width = 80;

            //  CallFunction.SelectValue2Text("customer", DataGV.Columns[4].Displayed.ToString);
            //  SelectValue2Text("customer", DataGV.Columns[4].ToString)

            DataGV.Columns[0].Visible = false;
            DataGV.Columns[3].Visible = false;
            DataGV.Columns[4].Visible = false;
            //设置数据表格为只读
            DataGV.ReadOnly = true;
            //不允许添加行
            DataGV.AllowUserToAddRows = false;
            //背景为白色
            DataGV.BackgroundColor = Color.White;
            //只允许选中单行
            DataGV.MultiSelect = false;
            //整行选中
            DataGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            if (DataGV.Rows.Count > 0)
                DataGV.CurrentCell = DataGV.Rows[0].Cells[1];
        }

        private void Btn_add_Click(object sender, EventArgs e)
        {
            Users_add_Form register_Form = new Users_add_Form();
            //  this.Hide();
            register_Form.ShowDialog();
        }

        private void Btn_edit_Click(object sender, EventArgs e)
        {
            int Row_i = DataGV.CurrentCell.RowIndex;
            int Cell_i = DataGV.CurrentCell.ColumnIndex;

            if (DataGV.Rows.Count == 0)
                return;

            int rowindex = DataGV.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV.Rows[rowindex].Cells[0].Value);

            Users_edit_Form users_edit = new Users_edit_Form(id);
            //  this.Hide();
            users_edit.ShowDialog();
            Query();

            if (Row_i != 0)
                DataGV.CurrentCell = DataGV.Rows[Row_i].Cells[Cell_i];
        }

        private void Btn_delete_Click(object sender, EventArgs e)
        {
            if (DataGV.Rows.Count == 0)
                return;
            int rowindex = DataGV.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV.Rows[rowindex].Cells[0].Value);
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                string sqlValue = " DELETE FROM login_user WHERE id=@id " +
                    " DELETE FROM login_user_power WHERE parent_id=@id ";
                SqlParameter[] parameters = {
                new SqlParameter("@id", id)
                };
                int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                label_show.Text = count_id + "条记录,删除成功";
                Query();
            }
        }

        private void Btn_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Users_Load(object sender, EventArgs e)
        {
            this.Width = 1200;
            this.Height = 700;
            LoginUsersService loginUsersService = new LoginUsersService();
            this.ComboBox_UserID.DataSource = loginUsersService.GetList();
            this.ComboBox_UserID.DisplayMember = "login_user_name";
            this.ComboBox_UserID.ValueMember = "login_user_id";
            this.ComboBox_UserID.SelectedIndex = -1;

            Query();
            Query_module();
            if (DataGV_module.Rows.Count > 0)
            {
                int rowindex = DataGV.CurrentRow.Index;
                string login_user_id = this.DataGV.Rows[rowindex].Cells["login_user_id"].Value.ToString();

                // int rowindex = DataGV_module.CurrentRow.Index;
                String module_no = DataGV_module.Rows[0].Cells["module_no"].Value.ToString();
                Query_dtl(login_user_id,module_no);



            }

        }

        private void dataGV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Query_module();
        }

        private void btn_copy_Click(object sender, EventArgs e)
        {
            int rowindex = DataGV.CurrentRow.Index;
            int sid = Convert.ToInt32(DataGV.Rows[rowindex].Cells[0].Value);
            string login_user_id = this.DataGV.Rows[rowindex].Cells["login_user_id"].Value.ToString();

            string login_user_id_copy = "";

            if (!ComboBox_UserID.Equals(""))
            login_user_id_copy = ComboBox_UserID.SelectedValue.ToString();

            if (dataGV2.Rows.Count > 0)
            {
                if (MessageBox.Show("是否清空已有权限？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //删除用户权限
                    LoginUsersPowerService loginUsersPowerService = new LoginUsersPowerService();
                    loginUsersPowerService.Delete(login_user_id);

                }
            }
            else
            {
                if (ComboBox_UserID.Text.Equals(""))
                {
                    MessageBox.Show("被复制用户不能为空！");
                    return;
                } 
                    

                string sqlValue = "INSERT INTO login_user_power(parent_id,login_user_id,group_power_no,group_power_name,  " +
                 "       page_id,page_no,page_name,module_no,module_name,isadd,isedit,isdelete,isquery,isprint,ischeck,isbackcheck)  " +
                 " SELECT @parent_id,@login_user_id,a.group_power_no,a.group_power_name,   " +
                 "        a.page_id,a.page_no,a.page_name,a.module_no,a.module_name,a.isadd,a.isedit,a.isdelete,a.isquery,a.isprint,a.ischeck,a.isbackcheck  " +
                 " FROM login_user_power a " +
                 " WHERE  a.login_user_id=@login_user_id_copy  ";
                SqlParameter[] parameters = {
                  new SqlParameter("@parent_id",sid),
                  new SqlParameter("@login_user_id",login_user_id),
                  new SqlParameter("@login_user_id_copy",login_user_id_copy),
                };
                SqlHelper.ExecuteNonQuery(sqlValue, parameters);

            }

            int rowindex2 = DataGV_module.CurrentRow.Index;
            String module_no = DataGV_module.Rows[rowindex2].Cells["module_no"].Value.ToString();
            Query_dtl(login_user_id,module_no);
        }

        private void DataGV_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                DataGV.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                (e.RowIndex + 1).ToString(),
                DataGV.RowHeadersDefaultCellStyle.Font,
                rectangle,
                DataGV.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGV2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                dataGV2.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                (e.RowIndex + 1).ToString(),
                dataGV2.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGV2.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void Query_module()
        {
            string sqlValue = "SELECT a.id,a.page_no as module_no,a.page_name as module_name  " +
                "  FROM page_power a " +
                "  WHERE 1=1  " +
                "  AND parent_id = @parent_id " +
                "  ORDER BY a.order_no  ";
            SqlParameter[] parameters = {
                new SqlParameter("@parent_id", "0"),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            if (ds.Tables.Count > 0)
            {
                DataGV_module.DataSource = ds.Tables[0];

                DataGV_module.Columns["id"].HeaderText = "ID";
                DataGV_module.Columns["module_no"].HeaderText = "模块编号";
                DataGV_module.Columns["module_name"].HeaderText = "模块";

                DataGV_module.Columns["id"].Width = 70;
                DataGV_module.Columns["module_no"].Width = 70;
                DataGV_module.Columns["module_name"].Width = 100;

                DataGV_module.Columns["id"].Visible = false;
                DataGV_module.Columns["module_no"].Visible = false;

                //不允许添加行
                DataGV_module.AllowUserToAddRows = false;
                //背景为白色
                DataGV_module.BackgroundColor = Color.White;
                //只允许选中单行
                DataGV_module.MultiSelect = false;
                //整行选中
                DataGV_module.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                // 列头隐藏
                // DataGV_module.ColumnHeadersVisible = false;
                // 行头隐藏
                //DataGV_module.RowHeadersVisible = false;
            }
        }

        private void DataGV_module_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = DataGV.CurrentRow.Index;
            String login_user_id = DataGV.Rows[rowindex].Cells["login_user_id"].Value.ToString();
            int rowindex2 = DataGV_module.CurrentRow.Index;
            String module_no = DataGV_module.Rows[rowindex2].Cells["module_no"].Value.ToString();
            Query_dtl(login_user_id,module_no);
        }

        public void Query_dtl(string login_user_id,string module_no)
        {
            if (DataGV.Rows.Count == 0)
                return;
            int rowindex = DataGV.CurrentRow.Index;
            int sid = Convert.ToInt32(DataGV.Rows[rowindex].Cells["id"].Value);

            //begin 显示权限群组
            string sqlValue = "SELECT a.id,a.module_name,a.page_no,a.page_name," +
              "  a.isadd,a.isedit,a.isdelete,a.isquery,a.isprint,a.ischeck,a.isbackcheck,a.isself " +
              "  FROM login_user_power a LEFT OUTER JOIN page_power b ON a.page_no = b.page_no " +
              "  WHERE 1=1  " +
             // " and a.module_name <> '用户与权限'" +
              " and a.module_no = case when @module_no <> '' then @module_no else a.module_no end " +
              " and a.login_user_id =  @login_user_id  " +
              " and a.parent_id =  @parent_id " +
              " ORDER BY order_no  ";
            SqlParameter[] parameters = {
                new SqlParameter("@parent_id", sid),
                new SqlParameter("@login_user_id", login_user_id),
                new SqlParameter("@module_no", module_no),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            if (ds.Tables.Count > 0)
            {
                dataGV2.DataSource = ds.Tables[0];
                dataGV2.Columns[0].HeaderText = "ID";
                dataGV2.Columns["module_name"].HeaderText = "模块";
                dataGV2.Columns["page_no"].HeaderText = "编号";
                dataGV2.Columns["page_name"].HeaderText = "页面";
                dataGV2.Columns["isadd"].HeaderText = "新增";
                dataGV2.Columns["isedit"].HeaderText = "修改";
                dataGV2.Columns["isdelete"].HeaderText = "删除";
                dataGV2.Columns["isquery"].HeaderText = "查询";
                dataGV2.Columns["isprint"].HeaderText = "打印";
                dataGV2.Columns["ischeck"].HeaderText = "审核";
                dataGV2.Columns["isbackcheck"].HeaderText = "弃审";
                dataGV2.Columns["isself"].HeaderText = "仅操作自己";

                dataGV2.Columns[0].Width = 60;
                dataGV2.Columns["module_name"].Width = 60;
                dataGV2.Columns["page_no"].Width = 60;
                dataGV2.Columns["page_name"].Width = 110;
                dataGV2.Columns["isadd"].Width = 45;
                dataGV2.Columns["isedit"].Width = 45;
                dataGV2.Columns["isdelete"].Width = 45;
                dataGV2.Columns["isquery"].Width = 45;
                dataGV2.Columns["isprint"].Width = 45;
                dataGV2.Columns["ischeck"].Width = 45;
                dataGV2.Columns["isbackcheck"].Width = 45;
                dataGV2.Columns["isself"].Width = 100;

                dataGV2.Columns[0].Visible = false;
                dataGV2.Columns["isself"].Visible = false;
                dataGV2.Columns["page_no"].Visible = false;
                dataGV2.Columns["module_name"].Visible = false;
                //设置数据表格为只读
                dataGV2.ReadOnly = true;
                //不允许添加行
                dataGV2.AllowUserToAddRows = false;
                //背景为白色
                dataGV2.BackgroundColor = Color.White;
                //只允许选中单行
                dataGV2.MultiSelect = false;
                //整行选中
                dataGV2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            //end 显示权限群组
        }

        private void DataGV_module_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                DataGV_module.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                (e.RowIndex + 1).ToString(),
                DataGV_module.RowHeadersDefaultCellStyle.Font,
                rectangle,
                DataGV_module.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }

}
